package gui;

import java.awt.Dimension;
import java.awt.TextArea;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import ge.escola.Sala;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.awt.Color;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AlteraSala extends javax.swing.JPanel {
	private JLabel descricao;
	private JLabel labeldescricao;
	private JButton botaoVoltar;
	private JLabel campoUm;
	private JLabel checaDescricao;
	private JButton botaoAltera;
	private TextArea areaDescricao;
	public static int posicao;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new AlteraSala());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public AlteraSala() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(800, 600));
			this.setSize(800, 600);
			this.setBackground(new java.awt.Color(204,204,255));
			this.setLayout(null);
			{
				descricao = new JLabel();
				this.add(descricao);
				descricao.setText("Alterar Sala");
				descricao.setBounds(317, 12, 193, 47);
				descricao.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				descricao.setForeground(new java.awt.Color(0,104,104));
				descricao.setFont(new java.awt.Font("Arial",0,36));
			}
			{
				labeldescricao = new JLabel();
				this.add(labeldescricao);
				labeldescricao.setText("Descricao");
				labeldescricao.setBounds(350, 90, 89, 30);
				labeldescricao.setForeground(new java.awt.Color(0,104,104));
				labeldescricao.setFont(new java.awt.Font("Arial",0,18));
			}
			{
				areaDescricao = new TextArea();
				this.add(areaDescricao);
				areaDescricao.setBounds(114, 154, 422, 363);
				areaDescricao.setFont(new java.awt.Font("Arial",0,18));
				areaDescricao.setText(GerenciadorDeJanelas.todasSalas.get(posicao).getDescricao());
				areaDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
		            public void keyReleased(java.awt.event.KeyEvent evt) {
		                caixaDescricaoKeyReleased(evt);
		            }
		        });
			}
			{
				botaoAltera = new JButton();
				this.add(botaoAltera);
				botaoAltera.setText("Alterar");
				botaoAltera.setBounds(635, 195, 99, 28);
				botaoAltera.setFont(new java.awt.Font("Arial",2,18));
				botaoAltera.setBackground(new java.awt.Color(255,255,255));
				botaoAltera.addMouseListener(new java.awt.event.MouseAdapter() {
		            public void mouseClicked(java.awt.event.MouseEvent evt){
		                try{
		                	alteraMouseClicked(evt);}
		                catch(Exception e){
		                	System.out.println(e.getMessage());
		                }
		            }
		        });
			}
			{
				botaoVoltar = new JButton();
				this.add(botaoVoltar);
				botaoVoltar.setText("Voltar");
				botaoVoltar.setBounds(635, 265, 99, 27);
				botaoVoltar.setFont(new java.awt.Font("Arial",2,18));
				botaoVoltar.setBackground(new java.awt.Color(255,255,255));
				botaoVoltar.addMouseListener(new java.awt.event.MouseAdapter() {
		            public void mouseClicked(java.awt.event.MouseEvent evt){
		                try{
		                	voltarMouseClicked(evt);}
		                catch(Exception e){
		                	System.out.println(e.getMessage());
		                }
		            }
		        });
			}
			{
				checaDescricao = new JLabel();
				this.add(checaDescricao);
				checaDescricao.setBounds(548, 237, 75, 23);
				checaDescricao.setFont(new java.awt.Font("Arial",0,18));
			}
			{
				campoUm = new JLabel();
				this.add(campoUm);
				campoUm.setBounds(37, 136, 722, 426);
				campoUm.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void alteraMouseClicked(java.awt.event.MouseEvent evt){
		String descricao = areaDescricao.getText();
		if(descricao.equals("") || descricao.replace(" ", "").length()==0){
			JOptionPane.showMessageDialog(null,"Descricao invalida");
			
		}
		else{
			try{
			    GerenciadorDeJanelas.todasSalas.get(posicao).setDescricao(areaDescricao.getText());
				GerenciadorDeJanelas.window.setVisible(false);
		        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuAltera);
		        GerenciadorDeJanelas.window.setVisible(true);
		        // transforma em xml
				String xmlSala = GerenciadorDeJanelas.ARQUIVADOR
						.toXML(GerenciadorDeJanelas.todasSalas);
				// grava o arrayXml no arquivo
				GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoDeSalas,
						xmlSala);
				}
			catch(Exception e){
				JOptionPane.showMessageDialog(null,"Erro");
			}
		}
	}
	
	public void voltarMouseClicked(java.awt.event.MouseEvent evt){
		GerenciadorDeJanelas.window.setVisible(false);
        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuAltera);
        GerenciadorDeJanelas.window.setVisible(true);
	}
	
	public void caixaDescricaoKeyReleased(java.awt.event.KeyEvent evt){
		String descricao = areaDescricao.getText();
		if(descricao.equals("") || descricao.replace(" ", "").length()==0){
			checaDescricao.setForeground(Color.RED);
			checaDescricao.setText("Invalido");
		}
		else{
			checaDescricao.setForeground(Color.GREEN);
			checaDescricao.setText("Ok");
		}
		
	}

}
